package com.task.util;

import com.task.model.CommonParameter;
import com.ximpleware.AutoPilot;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

public class VTDReader {


    public List<Map<String,Object>> readTaskXML(){
        try {
            String path = this.getClass().getResource("/").getPath();
            File f = new File(path + CommonParameter.TASKPATH);
            FileInputStream fis = new FileInputStream(f);
            byte[] ba = new byte[(int) f.length()];
            fis.read(ba);
            VTDGen vg = new VTDGen();
            vg.setDoc(ba);
            vg.parse(true);

            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(); // ap is going to move the cursor
            ap.bind(vn);
            ap.selectXPath("/task-list/task");
            int i;
            while((i=ap.evalXPath())!=-1){
                 System.out.println(vn.toString(vn.getAttrVal("id")));
                 System.out.println(vn.toString(vn.getAttrVal("class")));

            }
        }
        catch (Exception e){
           e.printStackTrace();
        }
        return null;
    }

    @Test
    public void test(){
            new  VTDReader().readTaskXML();
    }

}
